home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / DumbEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-08  |  9.8 KB  |  538 lines  |  [TEXT/KAHL]

  1. /*
  2.     DumbEdit - Multiple-window TransEdit Demonstration.
  3.  
  4.     The project should include DumbEdit.c (this file), TransEdit.c,
  5.     FakeAlert.c, TransSkel.c (or a project made from TransSkel.c)
  6.     and MacTraps.
  7.  
  8.     28 October 1986        Paul DuBois
  9. */
  10.  
  11. # include    <MenuMgr.h>
  12. # include    <FontMgr.h>
  13. # include    "TransEdit.h"
  14.  
  15.  
  16. # define    maxSize        8        /* no. font sizes made available */
  17. # define    hSize        300        /* horiz, vert size of new windows */
  18. # define    vSize        205
  19. # define    aboutAlrt    1000
  20.  
  21.  
  22. typedef enum        /* File menu item numbers */
  23. {
  24.     new = 1,        /* begin new window */
  25.     open,            /* open existing file */
  26.     close,            /* close file */
  27.     /* --- */
  28.     save = 5,        /* save file */
  29.     saveAs,            /* save under another name */
  30.     saveCopy,        /* save a copy w/o switching file binding */
  31.     revert,            /* revert to version on disk */
  32.     /* --- */
  33.     quit = 10
  34. };
  35.  
  36.  
  37. typedef enum            /* Edit menu item numbers */
  38. {
  39.     undo = 1,
  40.     /* --- */
  41.     cut = 3,
  42.     copy,
  43.     paste,
  44.     clear
  45. };
  46.  
  47.  
  48.  
  49. typedef enum        /* Format menu item numbers */
  50. {
  51.     wordWrap = 1,
  52.     noWrap,
  53.     /* --- */
  54.     leftJust = 4,
  55.     centerJust,
  56.     rightJust
  57. };
  58.  
  59.  
  60. WindowPtr    lastFront = nil;    /* keeps track of front window */
  61. MenuHandle    fileMenu;
  62. MenuHandle    editMenu;
  63. MenuHandle    fontMenu;
  64. MenuHandle    sizeMenu;
  65. MenuHandle    formatMenu;
  66.  
  67. int        sizes[maxSize] = { 9, 10, 12, 14, 18, 20, 24, 48 };
  68.  
  69.  
  70. /*
  71.     Uncheck all the items in a menu
  72. */
  73.  
  74. UncheckMenu (theMenu)
  75. MenuHandle    theMenu;
  76. {
  77. register int    i, nItems;
  78.  
  79.     nItems = CountMItems (theMenu);
  80.  
  81.     for (i = 1; i <= nItems; ++i)
  82.     {
  83.         CheckItem (theMenu, i, false);
  84.         SetItemStyle (theMenu, i, 0);
  85.     }
  86. }
  87.  
  88.  
  89. /*
  90.     Set the Font, Size and Format menus so that the items corresponding
  91.     to the text characteristics of the window are checked.  If the
  92.     window isn't an edit window, dim all three menus.
  93. */
  94.  
  95. SetTextMenus (drawBar)
  96. Boolean        drawBar;
  97. {
  98. WindowPtr    theWind;
  99. Str255        wFontName;
  100. Str255        mFontName;
  101. register int    i, nItems;
  102. register TEHandle    te;
  103.  
  104.     theWind = FrontWindow ();
  105.     UncheckMenu (fontMenu);                /* toss current check marks */
  106.     UncheckMenu (sizeMenu);
  107.     UncheckMenu (formatMenu);
  108.  
  109.     if (!IsEWindow (theWind))            /* disable the menus */
  110.     {
  111.         DisableItem (fontMenu, 0);
  112.         DisableItem (sizeMenu, 0);
  113.         DisableItem (formatMenu, 0);
  114.     }
  115.     else
  116.     {
  117.         EnableItem (fontMenu, 0);
  118.         EnableItem (sizeMenu, 0);
  119.         EnableItem (formatMenu, 0);
  120.  
  121.         te = GetEWindowTE (theWind);
  122.  
  123. /*
  124.     Check appropriate word wrap item
  125. */
  126.  
  127.         CheckItem (formatMenu, (**te).crOnly < 0 ? noWrap : wordWrap, true);
  128.  
  129. /*
  130.     Check appropriate justification item
  131. */
  132.  
  133.         switch ((**te).just)
  134.         {
  135.  
  136.         case teJustLeft:
  137.             CheckItem (formatMenu, leftJust, true);
  138.             break;
  139.  
  140.         case teJustRight:
  141.             CheckItem (formatMenu, rightJust, true);
  142.             break;
  143.  
  144.         case teJustCenter:
  145.             CheckItem (formatMenu, centerJust, true);
  146.             break;
  147.  
  148.         }
  149.  
  150. /*
  151.     Check appropriate font size item, and outline items for sizes
  152.     present in resource files
  153. */
  154.  
  155.         for (i = 0; i < maxSize; ++i)
  156.         {
  157.             if ((**te).txSize == sizes[i])
  158.                 CheckItem (sizeMenu, i + 1, true);
  159.  
  160.             if (RealFont ((**te).txFont, sizes[i]))
  161.                 SetItemStyle (sizeMenu, i + 1, outline);
  162.             else
  163.                 SetItemStyle (sizeMenu, i + 1, 0);        /* plain */
  164.         }
  165.  
  166. /*
  167.     Check appropriate font name item
  168. */
  169.         
  170.         GetFontName ((**te).txFont, wFontName);    /* name of window font */
  171.         nItems = CountMItems (fontMenu);        /* # fonts in menu */
  172.         for (i = 1; i <= nItems; ++i)
  173.         {
  174.             GetItem (fontMenu, i, mFontName);    /* get font name */
  175.             if (EqualString (wFontName, mFontName, false, true))
  176.             {
  177.                 CheckItem (fontMenu, i, true);
  178.                 break;
  179.             }
  180.         }
  181.  
  182.     }
  183.  
  184.     if (drawBar)
  185.         DrawMenuBar ();
  186. }
  187.  
  188.  
  189.  
  190. /*
  191.     Set File/Edit menu items according to type of front window.
  192.  
  193.     The general behavior is:
  194.  
  195.     New and Open always enabled, since a new edit window can always be
  196.     opened.
  197.  
  198.     Close enabled when an edit or DA window in front (i.e., when there's
  199.     a window at all).
  200.  
  201.     Save enabled for edit windows not bound to a file, and edit windows
  202.     bound to a file when they're dirty (typed into, Edit menu used to
  203.     do something to them).
  204.  
  205.     Save As and Save a Copy As enabled for edit windows.
  206.  
  207.     Revert enabled for edit windows bound to a file when they're dirty.
  208.  
  209.     Undo disabled when there's an edit window in front.
  210. */
  211.  
  212. SetNonTextMenus ()
  213. {
  214. WindowPtr    theWind;
  215. int            theKind;
  216.  
  217.     DisableItem (fileMenu, close);    /* assume no window at all */
  218.     DisableItem (fileMenu, save);
  219.     DisableItem (fileMenu, saveAs);
  220.     DisableItem (fileMenu, saveCopy);
  221.     DisableItem (fileMenu, revert);
  222.     EnableItem (editMenu, undo);
  223.  
  224.     theKind = 0;
  225.     if ((theWind = FrontWindow ()) != nil)
  226.         theKind = ((WindowPeek) theWind)->windowKind;
  227.  
  228.     if (theKind < 0)                        /* DA in front */
  229.     {
  230.         EnableItem (fileMenu, close);
  231.     }
  232.     else if (IsEWindow (theWind))            /* edit window in front */
  233.     {
  234.         EnableItem (fileMenu, close);
  235.         EnableItem (fileMenu, saveAs);
  236.         EnableItem (fileMenu, saveCopy);
  237.         if (GetEWindowFile (theWind, nil) == false)    /* not bound to file */
  238.         {
  239.             EnableItem (fileMenu, save);
  240.         }
  241.         else if (IsEWindowDirty (theWind))    /* bound - is it dirty? */
  242.         {
  243.             EnableItem (fileMenu, save);
  244.             EnableItem (fileMenu, revert);
  245.         }
  246.         DisableItem (editMenu, undo);
  247.     }
  248. }
  249.  
  250.  
  251. /*
  252.     Got an activate or deactivate.  It doesn't matter which, really.
  253.     Set the text menus appropriately for the front window, and draw
  254.     the menu bar, as these menus might change state from enabled to
  255.     disabled or vice-versa.
  256. */
  257.  
  258. Activate (active)
  259. Boolean    active;
  260. {
  261.     CheckFront ();
  262. }
  263.  
  264.  
  265. /*
  266.     Got a keyclick in an edit window.
  267. */
  268.  
  269. Key ()
  270. {
  271.     SetNonTextMenus ();
  272. }
  273.  
  274.  
  275. /*
  276.     Close selected from File menu, or close box of edit window
  277.     clicked.
  278. */
  279.  
  280. Close ()
  281. {
  282. WindowPtr    theWind;
  283.  
  284.     GetPort (&theWind);
  285.     (void) EWindowClose (theWind);
  286.     CheckFront ();
  287. }
  288.  
  289.  
  290. MakeWind (bindToFile)
  291. Boolean    bindToFile;
  292. {
  293. Rect        r;
  294. static int    windCount = 0;
  295. int            offset;
  296.  
  297.     if (FrontWindow () == nil)
  298.         windCount = 0;
  299.     SetRect (&r, 0, 0, hSize, vSize);
  300.     offset = 50 + 25 * (windCount++ % 4);
  301.     OffsetRect (&r, offset, offset);
  302.     (void) NewEWindow (&r, nil, true, -1L, true, 0L, bindToFile);
  303. }
  304.  
  305.  
  306. /*
  307.     File menu handler
  308. */
  309.  
  310. DoFileMenu (item)
  311. int        item;
  312. {
  313. WindowPtr    theWind;
  314.  
  315.     theWind = FrontWindow ();
  316.     switch (item)
  317.     {
  318.  
  319.     case new:
  320.         MakeWind (false);
  321.         break;
  322.  
  323.     case open:
  324.         MakeWind (true);
  325.         break;
  326.  
  327.     case close:
  328.         if (IsEWindow (theWind))
  329.             (void) EWindowClose (theWind);
  330.         else
  331.             CloseDeskAcc (((WindowPeek) theWind)->windowKind);    /* DA in front */
  332.         break;
  333.  
  334.     case save:
  335.         (void) EWindowSave (theWind);
  336.         break;
  337.  
  338.     case saveAs:
  339.         (void) EWindowSaveAs (theWind);
  340.         break;
  341.  
  342.     case saveCopy:
  343.         (void) EWindowSaveCopy (theWind);
  344.         break;
  345.  
  346.     case revert:
  347.         (void) EWindowRevert (theWind);
  348.         break;
  349.  
  350.     case quit:
  351.         if (ClobberEWindows () == true)
  352.             SkelWhoa ();
  353.         break;
  354.  
  355.     }
  356.     SetNonTextMenus ();
  357. }
  358.  
  359.  
  360. /*
  361.     Handle Font menu items
  362. */
  363.  
  364. DoFontMenu (item)
  365. int        item;
  366. {
  367. int            font;
  368. TEHandle    te;
  369. WindowPtr    theWind;
  370. Str255        theFontName;
  371.  
  372.     theWind = FrontWindow ();
  373.     if ((te = GetEWindowTE (theWind)) == nil)
  374.         return;                /* not an edit window */
  375.     GetItem (fontMenu, item, theFontName);
  376.     GetFNum (theFontName, &font);
  377.     SetEWindowStyle (theWind, font, (**te).txSize, (**te).crOnly, (**te).just);
  378.     SetTextMenus (false);
  379.  
  380. }
  381.  
  382.  
  383. /*
  384.     Handle Size menu items
  385. */
  386.  
  387. DoSizeMenu (item)
  388. int        item;
  389. {
  390. TEHandle    te;
  391. WindowPtr    theWind;
  392.  
  393.     theWind = FrontWindow ();
  394.     if ((te = GetEWindowTE (theWind)) == nil)
  395.         return;                /* not an edit window */
  396.     SetEWindowStyle (theWind, (**te).txFont, sizes[item-1], (**te).crOnly, (**te).just);
  397.     SetTextMenus (false);
  398. }
  399.  
  400.  
  401. /*
  402.     Handle Format menu items
  403. */
  404.  
  405. DoFormatMenu (item)
  406. int        item;
  407. {
  408. int            font, size, just, wrap;
  409. TEHandle    te;
  410. WindowPtr    theWind;
  411.  
  412.     theWind = FrontWindow ();
  413.     if ((te = GetEWindowTE (theWind)) == nil)
  414.         return;                /* not an edit window */
  415.     font = (**te).txFont;
  416.     size = (**te).txSize;
  417.     just = (**te).just;
  418.     wrap = (**te).crOnly;
  419.  
  420.     switch (item)
  421.     {
  422.  
  423.     case wordWrap:
  424.         wrap = 0;
  425.         break;
  426.  
  427.     case noWrap:
  428.         wrap = -1;
  429.         break;
  430.  
  431.     case leftJust:
  432.         just = teJustLeft;
  433.         break;
  434.  
  435.     case centerJust:
  436.         just = teJustCenter;
  437.         break;
  438.  
  439.     case rightJust:
  440.         just = teJustRight;
  441.         break;
  442.     }
  443.     SetEWindowStyle (theWind, font, size, wrap, just);
  444.     SetTextMenus (false);
  445. }
  446.  
  447.  
  448. /*
  449.     Handle selection of About… item from Apple menu
  450. */
  451.  
  452. DoAbout ()
  453. {
  454.     (void) Alert (aboutAlrt, nil);
  455. }
  456.  
  457.  
  458. /*
  459.     Background procedure.  Check front window, reset menus if it
  460.     changes.  The menu bar doesn't need redrawing by SetTextMenus
  461.     if the previous and current front window are either both edit
  462.     windows or both not edit windows.  This check eliminates some
  463.     needless menu flashing.
  464. */
  465.  
  466. CheckFront ()
  467. {
  468.     if (FrontWindow () != lastFront)
  469.     {
  470.         SetNonTextMenus ();
  471.         if (IsEWindow (FrontWindow ()) == IsEWindow (lastFront))
  472.             SetTextMenus (false);
  473.         else
  474.             SetTextMenus (true);
  475.         lastFront = FrontWindow ();
  476.     }
  477. }
  478.  
  479.  
  480. main ()
  481. {
  482.  
  483. /*
  484.     Initialize TransSkel, create menus and install handlers.
  485. */
  486.  
  487.     SkelInit ();
  488.  
  489.     SkelApple ("\pAbout DumbEdit…", DoAbout);
  490.  
  491.     fileMenu = NewMenu (1000, "\pFile");
  492.     AppendMenu (fileMenu, "\pNew/N;Open.../O;Close/K;(-;Save/S;Save As...");
  493.     AppendMenu (fileMenu, "\pSave a Copy As...;Revert/R;(-;Quit/Q");
  494.     SkelMenu (fileMenu, DoFileMenu, nil);
  495.  
  496.     editMenu = NewMenu (1001, "\pEdit");
  497.     AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  498.     SkelMenu (editMenu, EWindowEditOp, nil);
  499.  
  500.     fontMenu = NewMenu (1002, "\pFont");
  501.     DisableItem (fontMenu, 0);
  502.     AddResMenu (fontMenu, 'FONT');
  503.     SkelMenu (fontMenu, DoFontMenu, nil);
  504.  
  505.     sizeMenu = NewMenu (1003, "\pSize");
  506.     DisableItem (sizeMenu, 0);
  507.     AppendMenu (sizeMenu, "\p9 Point;10 Point;12 Point;14 Point");
  508.     AppendMenu (sizeMenu, "\p18 Point;20 Point;24 Point; 48 Point");
  509.     SkelMenu (sizeMenu, DoSizeMenu, nil);
  510.  
  511.     formatMenu = NewMenu (1004, "\pFormat");
  512.     DisableItem (formatMenu, 0);
  513.     AppendMenu (formatMenu, "\pWord Wrap;No Word Wrap;(-;Left;Center;Right");
  514.     SkelMenu (formatMenu, DoFormatMenu, nil);
  515.  
  516.     SetNonTextMenus ();
  517.     SetTextMenus (true);
  518.  
  519. /*
  520.     Do TransEdit-specific setup:  set creator for any files created,
  521.     set default text style and event notification procedures for
  522.     new windows.
  523. */
  524.  
  525.     SetEWindowCreator ('DUMB');
  526.     SetEWindowStyle (nil, monaco, 9, 0, teJustLeft);
  527.     SetEWindowProcs (nil, Key, Activate, Close);
  528.  
  529. /*
  530.     Process events until user quits,
  531.     then clean up and exit
  532. */
  533.  
  534.     SkelBackground (CheckFront);
  535.     SkelMain ();
  536.     SkelClobber ();
  537. }
  538.